/**
 * Copyright (C) 2009 Matteo Merli <mm@merlimat.org>
 * 
 * $Id: PxlParametersList.java 17 2009-01-12 12:36:59Z matteo.merli $
 * $URL: http://pxl-lang.googlecode.com/svn/trunk/src/main/java/pxl/types/util/PxlParametersList.java $
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package pxl.types.util;

import java.lang.reflect.Method;
import java.util.List;

import pxl.types.annotations.Parameter;
import pxl.types.annotations.Parameters;

import com.google.common.collect.Lists;

public class PxlParametersList
{

    private List<PxlParameter> parameters = Lists.newArrayList();
    private int positionalParameters = 0;
    private int mandatoryParameters = 0;
    private boolean hasExactNumberOfParameters = true;
    private boolean hasTupleParam = false;
    private boolean hasMapParam = false;

    public static PxlParametersList getFromAnnotations( Parameters parameters )
    {
        PxlParametersList res = new PxlParametersList();

        for ( Parameter param : parameters.parameters() )
        {
            PxlParameter pxlParameter = new PxlParameter( param.name() );
            pxlParameter.setIsOptional( param.optional() );
            pxlParameter.setIsTuple( param.isTuple() );
            pxlParameter.setIsMap( param.isMap() );
            res.add( pxlParameter );
        }
//
//        System.out.println( "==========\ncount: " + res.parameters.size() );
//        System.out.println( "positionals: " + res.positionalParameters );
//        System.out.println( "mandatory: " + res.mandatoryParameters );
//        System.out.println( "exact number: " + res.hasExactNumberOfParameters );
//        System.out.println( "tuple: " + res.hasTupleParam );
//        System.out.println( "map: " + res.hasMapParam );
        return res;
    }

    public static PxlParametersList getFromJavaMethod( Method method )
    {
        PxlParametersList res = new PxlParametersList();
        int n = method.getParameterTypes().length;

        for ( int i = 0; i < n; i++ )
        {
            PxlParameter pxlParameter = new PxlParameter( null );
            res.add( pxlParameter );
        }

        return res;
    }

    public void add( PxlParameter p )
    {
        parameters.add( p );
        if ( !p.isOptional() )
        {
            if ( !p.isTuple() && !p.isMap() )
                mandatoryParameters = parameters.size();
        }
        else
            hasExactNumberOfParameters = false;

        if ( p.isTuple() )
            hasTupleParam = true;
        else if ( p.isMap() )
            hasMapParam = true;
        else
            ++positionalParameters;
    }

    public PxlParameter get( int idx )
    {
        return parameters.get( idx );
    }

    public List<PxlParameter> parameters()
    {
        return parameters;
    }

    public boolean hasExactNumberOfParameters()
    {
        return hasExactNumberOfParameters;
    }

    public int mandatoryParameters()
    {
        return mandatoryParameters;
    }

    public int positionalParameters()
    {
        return positionalParameters;
    }

    public boolean hasTupleParam()
    {
        return hasTupleParam;
    }

    public boolean hasMapParam()
    {
        return hasMapParam;
    }

}
